package com.example.helloworld.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.os.Handler;
import android.widget.TextView;

public class LifecycleLog {
	private Map<String, String> mapStatus;
	private List<String> listMethod;
	private static LifecycleLog instance =	new LifecycleLog();
	private static final String STATUS_SUFFIX = "ed";
	
	private LifecycleLog() {
		mapStatus =		new HashMap<String, String>();
	    listMethod = 	new ArrayList<String>();
	}
	  
	public static LifecycleLog getInstance() {
		return instance;
	} 

	public List<String> getMethodList() {
	    return listMethod;
	}

	public void clear() {
	    listMethod.clear();
	    mapStatus.clear();
	}

	/**
	 * Adds the status value for the given activityName into the Map.
	 *
	 * @param activityName
	 * @param status
	 */
	public void setStatus(String activityName, String status) {
	    listMethod.add(activityName + "." + status + "()");
	    mapStatus.put(activityName, status);
	}

	/**
	 * Gets the status value for the given activityName.
	 *
	 * @param activityName
	 * @return
	 */
	public String getStatus(String activityName) {
	    String status = mapStatus.get(activityName);
	    status = status.substring(2, status.length()); // remove prefix "on"

	    // String manipulation to ensure the status value is spelled correctly.
	    if (status.endsWith("e")) { // create ...
	      status = status.substring(0, status.length() - 1);
	    }
	    if (status.endsWith("p")) { // stop ...
	      status = status + "p";
	    }
	    status = status + STATUS_SUFFIX;
	    return status;
	}
	  	
	public Set<String> keySet() {
	    return mapStatus.keySet();
	}

	public static void addLog(final String activityName, final String method) {
		instance.setStatus(activityName, method);
	}
	
	/**
	 * Helper method to print out the lifecycle state of each Activity.  Note this has
	 * been wrapped in a Handler to delay the output due to overlaps in lifecycle state
	 * changes as one Activity launches another.
	 * @link http://developer.android.com/guide/topics/fundamentals/activities.html#CoordinatingActivities
	 * @param viewMethods TextView to list out the lifecycle methods called
	 * @param viewStatus TextView to list out the status of all Activity classes
	 */
	public static void printStatus(final TextView viewMethods, final TextView viewStatus) {
		final LifecycleLog log =	instance;
		
		Handler handler = new Handler();
	    handler.postDelayed(new Runnable() {
	        public void run() {
	        	// Get the stack of Activity lifecycle methods called and print to TextView
	        	StringBuilder stringBuilder;
	        	stringBuilder = 			new StringBuilder();
	        	List<String> listMethods =	log.getMethodList();
	        	
	        	for (String method : listMethods) {
	        		stringBuilder.insert(0, method + "\r\n");
	        	}
	        	if (viewMethods != null) {
	        		viewMethods.setText(stringBuilder.toString());
	        	}

	        	// Get the status of all Activity classes and print to TextView
	        	stringBuilder = 			new StringBuilder();
	        	for (String key : log.keySet()) {
	        		stringBuilder.insert(0, key + ": " + log.getStatus(key) + "\n");
	        	}
	        	if (viewStatus != null) {
	        		viewStatus.setText(stringBuilder.toString());
	        	}
	        }
	    }, 750);
	}	
	
}
